// 引入服务器插件
const express = require("express");
// 创建服务对象
const app = express();
// 引入上传文件处理模块
const multer = require("multer");
// 创建文件处理实例，并配置上传后的文件的存储位置
const m = multer({dest: "./upload"});
// 引入各种路由模块
const registerRoute = require("./routers/register");
const loginRoute = require("./routers/login");
const getUserRoute = require("./routers/getUser");
const setUserRoute = require("./routers/setUser");
const setPasswordRoute = require("./routers/setPassword");
const setAvatarRoute = require("./routers/setAvatar");
const exportUserInfoRoute = require("./routers/exportUserInfo");
const importUserInfoRoute = require("./routers/importUserInfo");
const getTeamRoute = require("./routers/getTeam");
const setTeamRoute = require("./routers/setTeam");
// 注册各种中间件
app.use( express.static("./www") );
app.use( express.static("./upload") );
app.use( express.static("./export") );
app.use( express.urlencoded() );
app.use( m.any() );

// 配置CORS，允许跨域
app.all("*", (req,res,next)=>{
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'get,post')
  res.header('Access-Control-Allow-Headers', 'Content-Type')
  next();
})
// 之后的路由可以直接跨域请求

// http://10.11.50.50:3000/getteam

// 注册各种路由模块
app.post( "/register", registerRoute );
app.get( "/login", loginRoute );
app.get( "/getuser", getUserRoute );

app.post( "/setuser", setUserRoute );
app.post( "/setpassword", setPasswordRoute );
app.post( "/setavatar", setAvatarRoute );

app.get( "/exportuserinfo", exportUserInfoRoute );
app.post( "/importuserinfo", importUserInfoRoute );

app.get( "/getteam", getTeamRoute );
app.post( "/setteam", setTeamRoute );

// 将服务对象挂载到指定端口
app.listen(3000, ()=>{
  console.log("服务器开启成功");
})

// 响应数据规范：json格式
// code属性：
// 0：表示缺少必传参数
// 1：表示成功
// 2：表示数据库出错
// msg属性：
// 表示code的提示信息
// data属性：
// 表示本次请求的响应数据


